package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.adapter.MyMessageAdapter;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.MesgDeleteBean;
import com.yiwei.ydd.api.bean.MesgListBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.constant.Const;
import com.yiwei.ydd.util.Dialog;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.view.OnLoadeMoreListener;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyMessageActivity extends BaseActivity implements MyMessageAdapter.OnChooseListener {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private Unbinder unbiner;
    private MyMessageAdapter adapter;
    private String mess_type;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getMesgLists(page);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的消息");
        mess_type = getIntent().getStringExtra("type");
        txtRight.setVisibility(View.VISIBLE);
        txtRight.setText("清空");
        switch (mess_type) {
            case Const.MsgType.COMM:

                txtTitle.setText("佣金消息");
                break;
            case Const.MsgType.MONEY:

                txtTitle.setText("钱包消息");
                break;
            case Const.MsgType.SYSTEM:

                txtTitle.setText("系统消息");
                break;
            case Const.MsgType.ORDERS:

                txtTitle.setText("订单消息");
                break;
        }

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getMesgLists(1);
            }
        });
        adapter = new MyMessageAdapter(this);
        adapter.setOnChooseListener(this);
        list.setAdapter(adapter);
        getMesgLists(1);
    }

    private void getMesgLists(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        MesgListBean bean = new MesgListBean();
        bean.page = page;
        bean.mesg_type = mess_type;
        Api.api_service.getMesgLists(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    private void getMsgDelete(String id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        MesgDeleteBean bean = new MesgDeleteBean();
        bean.id = id;
        bean.mesg_type = mess_type;
        Api.api_service.getMesgDelete(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "删除成功");
                    onLoadeMoreListener.setPage();
                    getMesgLists(1);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }


    @Override
    public void onClick(String id) {

    }

    @Override
    public void onDelete(String id) {
        getMsgDelete(id);
    }

    @OnClick({R.id.btn_back, R.id.txt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                Dialog.showDefaultDialog(this, "", "是否决定清空所有消息？", "取消", "清空", false, new Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {
                        getMsgDelete(adapter.allIds());
                    }

                    @Override
                    public void cancel() {

                    }
                });

                break;
        }
    }
}
